﻿/************************************************************************
*  Copyright @   All rights reserved.
*  GUID: f749b3a7-374c-4751-bc4a-f3811a3cac4a
*  计算机用户域： XIANGLIUBO-PC
*  计算机用户名： WuHao
*  创建者： 向柳波
*  创建时间： 2015-01-28 03:48:29
*  创建说明： 
*  ---------------------------------------------------------------------
*  更新者： 
*  更新时间： 2015-01-28 03:48:29
*  更新说明： 
************************************************************************/

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using Commands;
using Core.Unity;
using Core.Unity.Wpf;
using DbHelp;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Services;
using Views;

namespace Modules {
    public class MeteorologicalDataModule : ModuleBaseWpf {
        private readonly IRegionViewRegistry _regionViewRegistry;
        private ContainerWpf _container;
        private CommandManagerWpf _commands;

        public MeteorologicalDataModule(ContainerWpf container, CommandManagerWpf cmg, IRegionViewRegistry regionViewRegistry)
            : base(container, cmg) {
            _regionViewRegistry = regionViewRegistry;
            _container = container;
            _commands = cmg;
        }

        public override void Initialize() {
            base.Initialize();

            this.RegisterViews();
            this.CreateService();
            this.CreateCommands();
        }

        public void RegisterViews() {
            //// LeftCenterView  RightCenterView  LeftBottomView  RightBottomView
            _regionViewRegistry.RegisterViewWithRegion("LeftCenterView", typeof(DataTableView));
            _regionViewRegistry.RegisterViewWithRegion("RightCenterView", typeof(TextHintView));
            _regionViewRegistry.RegisterViewWithRegion("LeftBottomView", typeof(AdditionalCmdsView));
            _regionViewRegistry.RegisterViewWithRegion("RightBottomView", typeof(ToolbarView));
        }

        public void CreateService() {
            base.CreateService();

            string localMdb = System.Environment.CurrentDirectory + @"\LocalData.accdb";
            this.Regist<DbHelper>(new AccessHelper(localMdb));
            //this.Regist<SaveDataService>(new SaveDataService());
            ////注册网络请求服务 
            this.Regist<EarthquakeInfoService>(new EarthquakeInfoService());
            this.Regist<DisasterInfoService>(new DisasterInfoService());
            this.Regist<RainLiveInfoService>(new RainLiveInfoService());
            this.Regist<WXYTInfoService>(new WXYTInfoService());
            this.Regist<RadarCrefInfoService>(new RadarCrefInfoService());
            this.Regist<SYQInfoService>(new SYQInfoService());
            this.Regist<Pm25InfoService>(new Pm25InfoService());
            this.Regist<AISInfoService>(new AISInfoService());
            
            //////注册数据库
            WebDataContext db = new WebDataContext();
            db.OnModelCreatingEvent += this.OnModelCreatingEvent;
            db.Set<Models.LogInfo>().Any();
            this.Regist<DbContext>(db);
        }

        void OnModelCreatingEvent(ref DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Models.LogInfo>().ToTable("WEBDATA_LOG");
            Dictionary<string, object> allObjs = this.Container.GetAll();
            foreach (var item in allObjs) {
                if (item.Value is IInfoService) {
                    IInfoService captureDataService = item.Value as IInfoService; 
                    captureDataService.OnModelCreating(ref modelBuilder);
                }
            }
        }

        public void CreateCommands() {
            //base.CmdMgr.Add(); 
            _commands.Add("queryearthquakecommand", new CommandAdapter(new QueryEarthquakeCommand()));
            _commands.Add("querydisastercommand", new CommandAdapter(new QueryDisasterCommand()));
            _commands.Add("querysyqcommand", new CommandAdapter(new QuerySYQCommand()));
            _commands.Add("queryaiscommand", new CommandAdapter(new QueryAISCommand()));
            _commands.Add("queryrainlivecommand", new CommandAdapter(new QueryRainLiveCommand()));
            _commands.Add("querypm25command", new CommandAdapter(new QueryPM25Command()));
            _commands.Add("queryradarcrefcommand", new CommandAdapter(new QueryRadarCrefCommand()));
            _commands.Add("querywxytcommand", new CommandAdapter(new QueryWXYTCommand()));
            _commands.Add("savedatacommand", new CommandAdapter(new SaveDataCommand()));

            base.CreateCommands();
        }
    }
}
